import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:qunaimedical/components/edit_text_field.dart';
import 'package:qunaimedical/components/primary_button.dart';
import 'package:qunaimedical/components/primary_label.dart';
import 'package:qunaimedical/network/http.dart';
import 'package:qunaimedical/utils/common_util.dart';
import 'package:qunaimedical/utils/dialog_util.dart';
import 'package:qunaimedical/utils/sp_util.dart';
import 'package:qunaimedical/utils/toast_util.dart';
import 'package:qunaimedical/values/urls.dart';

/// 后续反馈
class FeedbackPage extends StatefulWidget {
  const FeedbackPage({Key? key}) : super(key: key);

  @override
  _FeedbackPageState createState() => _FeedbackPageState();
}

class _FeedbackPageState extends State<FeedbackPage> {
  TextEditingController _controller = TextEditingController();

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('后续反馈'),
      ),
      body: ListView(
        children: [
          PrimaryLabel('后续反馈'),
          EditTextField(
            maxLines: 5,
            maxLength: 400,
            hintText: '请描述您所遇到的困难',
            controller: _controller,
          ),
          PrimaryButton('提交', onPressed: _post),
        ],
      ),
    );
  }

  void _post() async {
    CommonUtil.hideKeyboard(context);
    String content = _controller.text.trim();
    if (content.isEmpty) {
      ToastUtil.show('内容不能为空');
      return;
    }
    FormData formData = FormData.fromMap({
      "autoTest": "autoLogin",
      "userName": SPUtil.getUserName(),
      "password": SPUtil.getPassword(),
      "questionType": "appOther",
      "reportctx": _controller.text.trim(),
    });
    try {
      DialogUtil.showWaitDialog(context);
      var response = await XHttp.instance.post(Urls.FEEDBACK, data: formData);
      if (mounted) {
        Navigator.pop(context);
        if (response.statusCode == 200) {
          Navigator.pop(context);
          ToastUtil.show('提交成功');
        }
      }
    } on DioError catch (e) {
      if (mounted) {
        Navigator.pop(context);
        CommonUtil.catchDioError(e);
      }
    }
  }
}
